<?php
namespace app\platform\controller\terminal;
use app\BaseController;
use app\common\logic\KefuLogic;
use app\common\model\ActivationCodeModel;
use app\common\model\AppModel;
use app\common\model\OrderModel;
use app\common\model\ShopAlPaymentModel;
use app\common\model\ShopModel;
use app\common\model\ShopPaymentModel;

class Shop extends BaseController{
    public function showAppSku()
    {
        if(null === $app = AppModel::find($this->request->appId)){
            return $this->error('应用不存在');
        }
        if($app->is_delete != 0) return $this->error('应用不存在');
        $sku=$app->sku;
        foreach ($sku as $k => $v){
            $info=$v['num'].($v['unit']==2 ? '个' : '').($v['unit']==1 ? '天' : ($v['unit'] ==2 ? '月' : '年'));
            $sku[$k]['info']=$info;
        }
        return $this->success(['sku'=>$sku]);
    }

    public function activateShop()
    {
        $activation_code=$this->request->param('activation_code');
        if(empty($activation_code)) return $this->error('请输入激活码');

        if(null === $activation_code_model = ActivationCodeModel::where([
                ['activation_code','=',$activation_code],
                ['is_delete','=',0]
            ])->find()){
            return $this->error('激活码不存在');
        }
        if($activation_code_model->app_id != $this->request->appId) return $this->error('激活码与商铺不匹配');
        if($activation_code_model->status != 0) return $this->error('激活码已使用，无法再次使用');
        if($activation_code_model->expire_time < $this->request->time()) return $this->error('激活码已过期');

        //商铺操作
        $shop=$this->request->shop;
        $shop->expire_time=($shop->expire_time < $this->request->time() ? $this->request->time() : $shop->expire_time) + $activation_code_model->valid_days * 86400;
        $shop->type=2;
        $shop->save();
        //激活码操作
        $activation_code_model->status = 1;
        $activation_code_model->use_time=$this->request->time();
        $activation_code_model->save();
        //添加一条记录 后期补充
        $order = new OrderModel();
        $order->shop_id=$this->request->shopId;
        $order->type=4;
        $order->app_name=$shop->app->iden;
        $order->num=$activation_code_model->valid_days;
        $order->price=$activation_code_model->price;
        $order->is_paid=1;
        $order->save();

        return $this->success('激活成功');
    }

    public function showPaymentStatus()
    {
        $type=1;
        if(null === $payment = ShopPaymentModel::find($this->request->shopId)){
            $is_setting=0;
        }else{
            $is_setting=1;
            $type=$payment->type;
        }
        $return=[
            'is_setting'=>$is_setting,
            'type'=>$type
        ];

        return $this->success($return);
    }

    public function showShopPayment()
    {
        if(null === $payment =ShopPaymentModel::find($this->request->shopId)){
            return $this->success([]);
        }
        $detail=[
            'mach_id'=>$payment->mach_id,
            'api_auth'=>$payment->api_auth,
            'api_cert'=>$payment->api_cert,
            'api_key'=>$payment->api_key,
        ];

        return $this->success(['detail'=>$detail]);
    }

    public function saveShopPayment()
    {

        $mach_id  =  trim($this->request->param('mach_id'));
        $api_auth = trim($this->request->param('api_auth'));
        $api_cert = trim($this->request->param('api_cert'));
        $api_key  = trim($this->request->param('api_key'));

        if(empty($mach_id)){
            return $this->error('商户ID不能为空');
        }
        if(empty($api_auth)){
            return $this->error('API秘钥不能为空');
        }
        if(empty($api_cert) ){
            return $this->error('证书CERT内容不能为空');
        }
        if(empty($api_key) ){
            return $this->error('证书key内容不能为空');
        }
        if(null === $payment = ShopPaymentModel::find($this->request->shopId)){
            $payment = new ShopPaymentModel();
            $payment->shop_id = $this->request->shopId;
        }
        $payment->type = 0;
        $payment->mach_id = $mach_id;
        $payment->api_auth = $api_auth;
        $payment->api_cert = $api_cert;
        $payment->api_key = $api_key;
        $payment->save();
        return $this->success('保存成功');
    }


    public function showAlShopPayment()
    {
        if(null === $payment =ShopAlPaymentModel::find($this->request->shopId)){
            return $this->success([]);
        }

        return $this->success(['detail'=>$payment]);
    }

    public function saveAlShopPayment()
    {

        $appid  =  trim($this->request->param('appid'));
        $rsaPrivateKey = trim($this->request->param('rsaPrivateKey'));
        $alipayPublicKey = trim($this->request->param('alipayPublicKey'));

        if(empty($appid)){
            return $this->error('应用ID不能为空');
        }
        if(empty($rsaPrivateKey)){
            return $this->error('商户设置的私钥不能为空');
        }
        if(empty($alipayPublicKey) ){
            return $this->error('自动生成的公钥不能为空');
        }
        if(null === $payment = ShopAlPaymentModel::find($this->request->shopId)){
            $payment = new ShopAlPaymentModel();
            $payment->shop_id = $this->request->shopId;
        }
        $payment->type = 0;
        $payment->appid = $appid;
        $payment->rsaPrivateKey = $rsaPrivateKey;
        $payment->alipayPublicKey = $alipayPublicKey;
        $payment->save();
        return $this->success('保存成功');
    }


    public function showKefu()
    {
        $kefu=KefuLogic::getKefu($this->request->shopId);
        return $this->success(['kefu'=>$kefu]);
    }

}
